package com.shaoshaohuo.app.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.CarListAdapter;
import com.shaoshaohuo.app.adapter.PayAccountAdapter;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.Car;
import com.shaoshaohuo.app.entity.CarListEntity;
import com.shaoshaohuo.app.entity.PayAccount;
import com.shaoshaohuo.app.entity.PayAccountEntity;
import com.shaoshaohuo.app.entity.SupportWithdrawWay;
import com.shaoshaohuo.app.entity.SupportWithdrawWayEntity;
import com.shaoshaohuo.app.net.HttpRequest;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cao on 2016/1/20.
 */
public class PayAccountActivity extends BaseActivity implements AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener {

    public static final String getAccount = "getAccount";

    private TopbarView mTopbarView;

    private XListView xlv_listview;
    private List<PayAccount> list = new ArrayList<>();
    private PayAccountAdapter adapter;

    private int defaultPosition = -1; //默认银行卡 在列表中的位置

    private Dialog deleteDialog;

    private boolean select;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_account);
        select = getIntent().getBooleanExtra(getAccount, false);
        initView();
        setUpView();
        requestData();
    }

    private void initView() {
        mTopbarView = (TopbarView) findViewById(R.id.topbar);
        mTopbarView.setCenterText("我的收款账户");
        mTopbarView.setLeftView(true, true);
        mTopbarView.setRightText("添加");
        mTopbarView.setRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(PayAccountActivity.this, AddPayAccountActivity.class), 8);
            }
        });

        xlv_listview = (XListView) findViewById(R.id.xlv_listview);
    }

    private void setUpView() {
        xlv_listview.setPullLoadEnable(false);
        xlv_listview.setPullRefreshEnable(false);

        xlv_listview.setOnItemClickListener(this);
        xlv_listview.setOnItemLongClickListener(this);
    }

    private void requestData() {
        startLoadingDialog();
        RequestService.getInstance().payAccountList(this ,false , PayAccountEntity.class, new HttpRequest.RequestListener() {
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if (resultData.isOk()) {
                    PayAccountEntity entity = (PayAccountEntity) resultData;
                    if (entity.data != null && entity.data.list != null) {
                        list.clear();
                        list.addAll(entity.data.list);
                        setAdapter();
                    }
                } else {
                    showToast(resultData.getMsg());
                }
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(R.string.please_check_network);
            }
        });
    }

    private void setAdapter() {
        if (adapter == null) {
            adapter = new PayAccountAdapter(this, list, false);
            xlv_listview.setAdapter(adapter);
        } else {
            adapter.setData(list);
        }

        for (int i = 0; i < list.size(); i++) {
            if("1".equals(list.get(i).isDefault)){
                defaultPosition = i;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            requestData();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        showDeleteDialog(position);
        return false;
    }

    private void showDeleteDialog(final int position) {
        if (deleteDialog != null && deleteDialog.isShowing()) {
            return;
        }
        if (deleteDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    deletePayAccount(position);
                }
            });

            builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (deleteDialog.isShowing()) {
                        deleteDialog.dismiss();
                    }
                }
            });
            builder.setTitle("删除");
            builder.setMessage("确定要删除此银行卡？");
            deleteDialog = builder.create();
        }
        deleteDialog.show();
    }

    private void deletePayAccount(final int position) {
        startLoadingDialog();
        String id = list.get(position-1).id;
        RequestService.getInstance().deletePayAccount(this, id, BaseEntity.class, new HttpRequest.RequestListener() {
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if (resultData.isOk()) {
                    showToast("删除成功");
                    list.remove(position);
                    setAdapter();
                }
            }
            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast("删除失败，请重试");
            }
        });
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
        if (select) {
            Intent intent = new Intent();
            intent.putExtra(ExtraName.account, list.get(position - 1));
            setResult(Activity.RESULT_OK, intent);
            finish();
            return;
        }
        startLoadingDialog();
        String listId = list.get(position - 1).id;
        RequestService.getInstance().setDefaultAccount(this, listId, BaseEntity.class, new HttpRequest.RequestListener() {
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if (resultData.isOk()) {
                    list.get(position - 1).isDefault = "0";
                    list.get(defaultPosition).isDefault = "1";
                    defaultPosition = position - 1;
                    setAdapter();
                }
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
            }
        });
    }
}

